package org.zjt.thread;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-06-01 下午5:27
 * @see
 */
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceTest {

    public static void main(String[] args) {

        Marker marker1 = new Marker(Arrays.asList("2"), 1);
        Marker marker2 = new Marker(Arrays.asList("2","4"), 2);


        AtomicReference atomicReference = new AtomicReference<>(marker1);
        Marker marker3 = (Marker) atomicReference.get();

        System.out.println("第一次得到的Marker对象:" + marker3);
        System.out.println("marker3和marker1是否相等:" + (marker3 == marker1));


        atomicReference.compareAndSet(marker1, marker2); // 执行交换


        Marker marker4 = (Marker) atomicReference.get();
        System.out.println("第二次得到的Marker对象:" + marker4);
        System.out.println("marker4和marker2是否相等:" + (marker4 == marker2));

        Marker marker5 = new Marker(Arrays.asList("22","4"), 3);
        atomicReference.compareAndSet(marker4, marker5); // 执行交换
        System.out.println("第二次得到的Marker对象:" + atomicReference.get());
    }
}


class Marker {


    private List list;

    private int id;

    public Marker(List list, int id) {
        this.list = list;
        this.id = id;
    }

    @Override
    public String toString() {
        return "id:=" + id + list;
    }
}